package com.aios.action.timer;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.aios.action.AbstractManagementAction;
import com.aios.entity.DaySchedule;
import com.aios.entity.Switch;
import com.aios.entity.Timer;
import com.aios.entity.mapper.AssignedState;
import com.aios.entity.mapper.TimerSetting;
import com.aios.entity.unit.DayName;
import com.aios.exception.ItemNotFoundException;
import com.aios.exception.NullReferenceAccessException;
import com.aios.service.SwitchService;
import com.aios.service.TimerService;

public abstract class AbstractTimerAction extends AbstractManagementAction {

	public static final int DAYS_PER_WEEK = 7;
	private final static int DAT_HOURS=24;
	private final static int MINUTE_VALUE=60;
	private List<String> time_hour;
	private List<String> time_min;
	private List<AssignedState> selectedWeekList;
	
	protected Timer timer;
	protected String hour;
	protected String min;
	protected TimerSetting timerSetting;
	protected List<String> settingList;
	protected List<Integer> weekList;
	protected List<Integer> dayList;

	public List<Switch> getList() {
		return list;
	}

	public void setList(List<Switch> list) {
		this.list = list;
	}

	public TimerSetting getTimerSetting() {
		return timerSetting;
	}

	public void setTimerSetting(TimerSetting timerSetting) {
		this.timerSetting = timerSetting;
	}

	private static final long serialVersionUID = 1L;
	@Qualifier("timerService")
	@Autowired protected TimerService timerService;
	protected List<Switch> list;
	
	@Qualifier("switchService")
	@Autowired protected SwitchService switchService;

	public Timer findById() {

		if (id != null) {
			try {
				return timerService.findById(id);
			} catch (Exception e) {
				throw new ItemNotFoundException("Id : " + id);
			}
		} else {
			// logger
			throw new NullReferenceAccessException("Id cannot be null");
		}
	}
	

	protected void populateSelectedDays() {
		
		selectedWeekList = new ArrayList<AssignedState>(DAYS_PER_WEEK);
		
		String state = Switch.CHECKBOX_UNCHECKED;
		
		int s;
		
		for(int i = 0; i < weekList.size(); i++) {
			state = Switch.CHECKBOX_UNCHECKED;
			s = weekList.get(i);
			
			if(dayList != null) {
				if(containKey(dayList, s)) {
					state = Switch.CHECKBOX_CHECKED;
				}
			} else {
				state = Switch.CHECKBOX_UNCHECKED;
			}
			
			selectedWeekList.add(new AssignedState((DayName.getMap(Locale.JAPAN).get(i)), i, state));
		}
	}
	
	private boolean containKey(List<Integer> l, Integer val) {
		
		for(Integer i : l) {
			if(i == val) {
				return true;
			}
		}
		
		return false;
	}
	
	public Timer getTimer() {
		return timer;
	}

	public void setTimer(Timer timer) {
		this.timer = timer;
	}
	public void inputPopulate(){
		list=switchService.findBySwitchPanel(id);
		
	}

	public List<String> getTime_hour() {
		return time_hour;
	}

	public void setTime_hour(List<String> time_hour) {
		this.time_hour = time_hour;
	}

	public List<String> getTime_min() {
		return time_min;
	}

	public void setTime_min(List<String> time_min) {
		this.time_min = time_min;
	}
	public void populateHourList(){
		time_hour = new ArrayList<String>();
		for(int i=0;i<DAT_HOURS;i++){
			String j=String.format("%02d", i);
			time_hour.add(j);

		}
	}
	
	public void populateMinList(){
		time_min = new ArrayList<String>();
		for(int i=0;i<MINUTE_VALUE;i+=5)
		{	
			String f= String.format("%02d", i);
			time_min.add(f);
			
		}
	}
	
	public void setHour(String hour) {
		this.hour = hour;
	}

	public void setMin(String min) {
		this.min = min;
	}
	
	public String getHour(){
		return hour;
	}
	
	public String getMin(){
		return min;
	}
	
	public void calTime(int time){
		hour=String.format("%02d",( time /  MINUTE_VALUE));
		min= String.format("%02d",( time %  MINUTE_VALUE));
	}
	
	public void setSettingList(){
		settingList=new ArrayList<String>();
		settingList.add(Timer.TIMERSETTING_ON);
		settingList.add(Timer.TIMERSETTING_OFF);
		settingList.add(Timer.TIMERSETTING_ON_OFF);
	}
	
	public void populateWeekList(){
		
		Map<Integer, String> dayMap = DayName.getMap(Locale.JAPAN);
		
		weekList = new ArrayList<Integer>();
		
		for(int i = 0; i < DAYS_PER_WEEK; i++) {
			weekList.add(i);
		}
	}

	public List<String> getSettingList() {
		return settingList;
	}

	public void setSettingList(List<String> settingList) {
		this.settingList = settingList;
	}

	protected void setDayListValues(TimerSetting timerSetting) {
		
		dayList = new ArrayList<Integer>();
		
		for(DaySchedule ds: timerSetting.getSchedule()) {
			dayList.add(ds.getDayOf().intValue());
		}
		
	}

	public List<Integer> getWeekList() {
		return weekList;
	}

	public void setWeekList(List<Integer> weekList) {
		this.weekList = weekList;
	}

	public List<AssignedState> getSelectedWeekList() {
		return selectedWeekList;
	}

	public void setSelectedWeekList(List<AssignedState> selectedWeekList) {
		this.selectedWeekList = selectedWeekList;
	}

	public List<Integer> getDayList() {
		return dayList;
	}

	public void setDayList(List<Integer> dayList) {
		this.dayList = dayList;
	}
	
}
